/**
 * @file cx_heap.h
 * @author Char_X (2421498578@qq.com)
 * @brief Implementation of memory management
 * @version 0.1 
 * @date 2023-03-19 Basic function of memory management by lists use macro CX_LIB_DISABLE_HEAP to disable default heap management
 * @note If you use disable macro, the cx_heap_init() function will invalid 
 *       and you should use x_heap_override_manager()to override manager function
 * 
 * @copyright Copyright (c) 2023
 * 
 */
#ifndef _CX_HEAP_H
#define _CX_HEAP_H
#include "cx_typedef.h"

typedef struct CX_HEAP_Block
{
    struct CX_HEAP_Block *NextFreeBlock;
    cx_size_t BlockSize;
}cx_BlockLink_t;

/**
 * @brief heap_initialized function
 * 
 * @param pool memory pool address
 * @param size pool size
 * @return pool free size
 */
cx_int32_t cx_heap_init(cx_byte * pool, cx_size_t size);

/**
 * @brief Memory allocation
 * 
 * @param size wanted size(Bytes)
 * @return void* 
 */
void * cx_malloc(cx_size_t size);

/**
 * @brief Free a memory block
 * 
 * @param ptr 
 */
void cx_free(void * ptr);

/**
 * @brief Get the avaliable size of a block
 * 
 * @param ptr 
 * @return cx_size_t 
 */
cx_size_t cx_getHeapBlockSize(void *ptr);

/**
 * @brief Override cx_malloc and cx_free function
 * 
 * @param malloc malloc function
 * @param free free function
 * @return int 0
 */
int cx_heap_override_manager(void* (*malloc)(cx_size_t), void (*free)(void*));

#endif